package com.fuint.common.util;

import cn.hutool.core.util.StrUtil;
import lombok.extern.slf4j.Slf4j;

import java.util.Objects;

/**
 * 日志类
 *
 * @author Tom
 * @since 2025/01/23
 */
@Slf4j
public class LogUtils {
    public final static String DEBUG = "debug";
    public final static String INFO = "info";
    public final static String ERROR = "error";
    public final static String WARN = "warn";

    public static void error(String title, Object... args) {
        StringBuilder builder = new StringBuilder();
        builder.append("\n");
        if (StrUtil.isNotEmpty(title)) {
            builder.append(title).append("\n");
        }
        if (Objects.nonNull(args)) {
            for (Object arg : args) {
                builder.append(arg).append("\n");
            }
        }
        log.error(builder.toString());
    }

    public static void error(String title, Throwable e) {
        StringBuilder builder = new StringBuilder();
        builder.append("\n");
        if (StrUtil.isNotEmpty(title)) {
            builder.append(title).append("\n");
        }
        String message = e.getMessage();
        if (StrUtil.isNotEmpty(message)) {
            builder.append("Exception Message: ").append(message).append("\n");
        }
        Throwable cause = e.getCause();
        if (cause != null) {
            builder.append("Cause: ").append(cause).append("\n");
        }
        Throwable[] suppressed = e.getSuppressed();
        if (suppressed.length > 0) {
            builder.append("Suppressed Exceptions: \n");
            for (Throwable suppressedException : suppressed) {
                builder.append("\t").append(suppressedException).append("\n");
            }
        }
        StackTraceElement[] stackTrace = e.getStackTrace();
        builder.append("StackTrace: \n");
        for (StackTraceElement element : stackTrace) {
            builder.append("\t").append(element).append("\n");
        }
        log.error(builder.toString());
    }

    public static void info(String title, String message) {
        log(INFO, "\n" + title + "\n" + message);
    }

    public static void info(String message) {
        log(INFO, "\n" + message);
    }

    public static void log(String logType, String message) {
        if (ERROR.equals(logType)) {
            log.error(message);
        } else if (WARN.equals(logType)) {
            log.warn(message);
        } else if (DEBUG.equals(logType)) {
            log.debug(message);
        } else {
            log.info(message);
        }
    }

    public static void log(String logType, String message, Object... args) {
        if (ERROR.equals(logType)) {
            log.error(message, args);
        } else if (WARN.equals(logType)) {
            log.warn(message, args);
        } else if (DEBUG.equals(logType)) {
            log.debug(message, args);
        } else {
            log.info(message, args);
        }
    }

    public static void info(String message, Object... args) {
        log.info("\n" + message, args);
    }
}
